App.controller('departmentController', ['$rootScope', '$scope', '$http', 'Page', function($rootScope, $scope, $http, Page) {
		
    $scope.query = function() {
        Page.ajaxPost('department/treeGrid.do', null, function(resp) {
            $scope.depts = resp.data;
        });
    }

    $scope.query();

    $scope.deleteDept = function (dept) {
        Page.confirm("确定要删除吗？", function() {
            Page.ajaxPost('department/delete.do', dept, function() {
                $scope.query();
            });
        });
    };
    $scope.editDept = function (dept) {
        $scope.dept = dept;
        $scope.edit = true;
    };

    $scope.save = function() {
        if($scope.dept.parentDeptId == undefined){
            Page.notice("上级部门不能为空");
            return false;
        }
        Page.ajaxPost($scope.dept.deptId ? 'department/update.do' : 'department/add.do', $scope.dept, function() {
            $scope.edit = false;
            $scope.query();
        });
    };

    $scope.cancel = function () {
        $scope.edit = false;
    };

     $scope.addRow = function () {
        $scope.edit = true;
        $scope.dept = {};
    };

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        $("#dept_table").treetable({ expandable: true }, true);
    });

}]);
